using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuadTreeExample
{
    public partial class Form1 : Form
    {
        QuadTree qt;
        List<Point> dotList;
        List<Rectangle> boxList;
        Rectangle selectedBox;
        Random rand = new Random();

        int drawWidth = 600;
        int drawHeight = 600;
        int treeDepth = 8;


        public Form1()
        {
            InitializeComponent();
            dotList = new List<Point>();
            boxList = new List<Rectangle>();
            selectedBox = new Rectangle();
        }

        private void buttonMake_Click(object sender, EventArgs e)
        {
            boxList = new List<Rectangle>();
            selectedBox = new Rectangle();
            dotList = new List<Point>();
            int dotCount = Int32.Parse(textBox1.Text);

            for (int i = 0; i < dotCount; i++)
            {
                dotList.Add(new Point(rand.Next(drawWidth), rand.Next(drawHeight)));

            }
            this.Invalidate();
        }

        private void buttonPartition_Click(object sender, EventArgs e)
        {
            buttonPartition.Enabled = false;
            boxList = new List<Rectangle>();
            qt = new QuadTree(drawWidth, drawHeight,treeDepth);
            foreach (Point dot in dotList)
            {
                qt.PutInTree(dot.X,dot.Y, dot);
            }


            boxList.Add(TraverseTree(qt.Root, boxList));
            this.Invalidate();
            buttonPartition.Enabled = true;
        }

        private Rectangle TraverseTree(QTNode qtNode, List<Rectangle> boxList)
        {
            Rectangle tempRect = new Rectangle();

            if (qtNode.HasChildren)
            {
                for (int i = 0; i < qtNode.Children.Length; i++)
                {
                    boxList.Add(TraverseTree(qtNode.Children[i], boxList));
                }

            }
            else
            {
                tempRect.X = (int)qtNode.XMin;
                tempRect.Y = (int)qtNode.YMin;
                tempRect.Width = (int)(qtNode.XMax - tempRect.X);
                tempRect.Height = (int)(qtNode.YMax - tempRect.Y);
            }
            return tempRect;
        }






        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            foreach (Point dot in dotList)
            {
                g.FillEllipse(Brushes.DarkBlue, dot.X-3, dot.Y-3, 7, 7);

            }


            foreach (Rectangle box in boxList)
            {
                g.DrawRectangle(Pens.DarkRed, box);
            }

                g.DrawRectangle(Pens.Yellow, selectedBox);
           
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.X < drawWidth && e.Y < drawHeight)
            {
                QTNode currNode = qt.GetNodeContaining(e.X, e.Y);
                label2.Text = "Contained Data:\r\n";
                if (currNode.Data != null)
                {
                    foreach (NodeData data in currNode.Data)
                    {
                        label2.Text += data.Data.ToString() + "\r\n";
                    }
                }
                selectedBox.X = (int)currNode.XMin;
                selectedBox.Y = (int)currNode.YMin;
                selectedBox.Width = (int)(currNode.XMax - selectedBox.X);
                selectedBox.Height = (int)(currNode.YMax - selectedBox.Y);

                this.Refresh();
            }
        }


    }
}